﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MooseIRC.Client.Server.utility;

namespace MooseIRC.Client.Server {

    /// <summary>
    /// Application Settings
    /// </summary>
    internal static class Settings {

        /// <summary>
        /// Imported Settings
        /// </summary>
        private static Dictionary<string, Dictionary<string, string>> m_settings;

        /// <summary>
        /// Initialize static members
        /// </summary>
        static Settings() {
            import_settings();
        }

        /// <summary>
        /// Get Settings
        /// </summary>
        internal static Dictionary<string, Dictionary<string, string>> MasterSettings {
            get {
                return m_settings;
            }
        }

        /// <summary>
        /// Import All settings, of all INI files, 
        /// Note redundant sections or settings may be overwritten, if files with duplicate
        /// sections or settings exist..
        /// I dont really care right now...
        /// </summary>
        private static void import_settings() {
            m_settings = new Dictionary<string, Dictionary<string, string>>(); //Always start fresh
            foreach (string config_file in Directory.GetFiles(Environment.CurrentDirectory, "*.ini", SearchOption.AllDirectories)) {

                //Merge Settings File into master settings
                Dictionary<string, Dictionary<string, string>> settings = INIUtility.ReadAll(config_file);
                foreach (string key in settings.Keys) {
                    if (!m_settings.ContainsKey(key))
                        m_settings[key] = new Dictionary<string, string>();
                    foreach (string sub_key in settings[key].Keys) {
                        m_settings[key][sub_key] = settings[key][sub_key];
                    }
                }
            }
        }

        /// <summary>
        /// Gets the default encoding
        /// </summary>
        internal static Encoding DEFAULT_ENCODING {
            get {
                return Encoding.UTF8;
            }
        }

    }
}
